package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.OrderConvert;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderApproveDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderReviewDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderDetailEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service("orderDetailWriteService")
@TaTransactional
public class OrderDetailWriteService extends BaseService {
	@Autowired
	private OrderDetailWriteDao orderDetailWriteDao;


	public void saveAuditDetail(OrderReviewDTO orderReviewDTO, UserAccountVo userAccountVo){

		OrderDetailEntity orderDetailEntity = OrderConvert.convertOrderDetailEntity(orderReviewDTO, userAccountVo);
		orderDetailEntity.setCheckType(Constant.CheckDatailStatus.he);
		orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.STREET);

		int rst;
		if(CommonUtil.invalidArgs(orderDetailEntity.getId())){
			orderDetailEntity.setId(Constant.getUUID());
			rst = orderDetailWriteDao.insert(orderDetailEntity);
		}else{
			rst = orderDetailWriteDao.updateById(orderDetailEntity);
		}
		if(rst <= 0){
			throw new BizException("-1", "保存审核信息失败");
		}
	}

	public int insertSalvationAudit(OrderDetailEntity orderDetailEntity){
		orderDetailEntity.setId(Constant.getUUID());
		return orderDetailWriteDao.insert(orderDetailEntity);
	}


	public int insertSalvationAudits(List<OrderDetailEntity> detailList){
		return orderDetailWriteDao.inserts(detailList);
	}


	public int updateSalvationAudit(OrderDetailEntity orderDetailEntity){
		int rst = orderDetailWriteDao.updateById(orderDetailEntity);
		if(rst <= 0){
			throw new BizException(FailureCode.ERR_9999);
		}
		return rst;
	}


	public void saveSalvationApprove(OrderApproveDTO orderApproveDTO, UserAccountVo userAccountVo){
		OrderDetailEntity orderDetailEntity = OrderConvert.convertOrderDetailEntity(orderApproveDTO, userAccountVo);
		int rst = 0;
		if(CommonUtil.invalidArgs(orderApproveDTO.getOrderDetailId())){
			orderDetailEntity.setId(Constant.getUUID());
			rst = orderDetailWriteDao.insert(orderDetailEntity);
		}else{
			orderDetailEntity.setId(orderApproveDTO.getOrderDetailId());
			rst = orderDetailWriteDao.updateById(orderDetailEntity);
		}
		if(rst <= 0){
			throw new BizException(FailureCode.ERR_9999);
		}
	}


	public int deleteByOrderId(String orderId){
		return orderDetailWriteDao.deleteByOrderId(orderId);
	}


	public void insert(OrderDetailEntity entity){
		int rst = orderDetailWriteDao.insert(entity);
		if(rst <= 0){
			throw new BizException(FailureCode.ERR_9999);
		}
	}


	public int inserts(List<OrderDetailEntity> detailList){
		return orderDetailWriteDao.inserts(detailList);
	}
}
